module.exports = (sequelize, DataTypes) => {
    const Part = sequelize.define(
        'Part',
        {
            id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                autoIncrement: true,
                primaryKey: true
            },
            no:{
                type:DataTypes.INTEGER.UNSIGNED,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '配件货号不能为空'
                    }
                },
                comment: '配件货号'
            },
            name: {
                type: DataTypes.STRING,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '配件名不能为空'
                    }
                },
                comment: '配件名字'
            },
            model: {
                type: DataTypes.STRING,
                comment: '配件型号'
            },
            category_id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                references: {
                    model: 'Category',
                    key: 'id'
                },
                validate: {
                    notNull: {
                        msg: '所属分类不能为空'
                    }
                },
                comment: '所属分类'
            }
        },
        {
            tableName: 'part'
        }
    )

    Part.associate = function(models) {
        models.Part.belongsTo(models.Category, { foreignKey: 'category_id', targetKey: 'id' })
    }

    return Part
}
